package service.pdfgeneration;

import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.List;
import org.springframework.stereotype.Service;
import service.configuration.FieldType;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.html.simpleparser.StyleSheet;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import web.form.Block;
import web.form.Item;
import web.form.SheetView;


@Service
public class PDFGenerator implements IPDFGenerator {
	
	
	private boolean blockIsEmpty(Block block) {
    	if(block.getItems() == null || block.getItems().isEmpty()) {
    		return true;
    	}
    	
    	boolean empty = true;
    	for(Item item : block.getItems()) {
    		if((item != null) && (item.getValue() != null) && (!item.getValue().trim().isEmpty())) {
    			empty = false;
    			break;
    		}
    	}
    	
    	return empty;
	}
	
	
	public void generatePdf(SheetView sheetView, OutputStream out) throws DocumentException, IOException {
	        Document document = new Document();
	        PdfWriter.getInstance(document, out);
	        
	        document.open();
			document.addTitle(sheetView.getCode() + " : " + sheetView.getTitle());
			document.addCreationDate();
			//document.setMargins(10, 10, 10, 10);
	        
	        StyleSheet styles = new StyleSheet();
			Font headerFont = new Font();
			headerFont.setColor(BaseColor.WHITE);

	        Paragraph title = new Paragraph(sheetView.getTitle());
	        Font titleFont = title.getFont();
	        titleFont.setSize(16);
	        titleFont.setStyle(Font.BOLD);
	        titleFont.setStyle(Font.UNDERLINE);
	        title.setAlignment(Element.ALIGN_CENTER);
	        title.setSpacingAfter(80);
	        title.setFont(titleFont);
	        document.add(title);
			
	        
	        for(Block block : sheetView.getBlocks()) {
	        	if(blockIsEmpty(block)) {
	        		continue;
	        	}
	        	
				PdfPTable table = new PdfPTable(1);
				table.setWidthPercentage(90);
				table.setSpacingAfter(30);
				table.setSplitRows(true);
				table.setSplitLate(false);
				
	        	PdfPCell blockCell = new PdfPCell(new Phrase(block.getLabel(), headerFont));
		        blockCell.setBackgroundColor(new BaseColor(38, 148, 232));
	        	blockCell.setHorizontalAlignment(Element.ALIGN_LEFT);
        		blockCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        		blockCell.setUseAscender(true);
        		blockCell.setPadding(6);
        		table.addCell(blockCell);
	        	
        		for(Item item : block.getItems()) {            		
	        		if(item.getValue() != null && !item.getValue().trim().isEmpty()) {
	        			PdfPCell itemCell = new PdfPCell(new Phrase(item.getLabel(), headerFont));
	        			itemCell.setBackgroundColor(new BaseColor(98, 187, 232));
	            		itemCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        			itemCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        			itemCell.setUseAscender(true);
	        			itemCell.setPadding(3);
		        		table.addCell(itemCell);
	        			
	            		PdfPCell cell = new PdfPCell();
	            		cell.setUseAscender(true);
	            		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	            		cell.setPadding(5);
	        			if(item.getType() == FieldType.text) {
	        				List<Element> elements = HTMLWorker.parseToList(
	        						new StringReader(item.getValue()), styles);
			        		for(Element element : elements) {
			        			cell.addElement(element);
			        		}
	        			} else {
	        				cell.addElement(new Phrase(item.getValue()));
	        			}
	        			table.addCell(cell);
	        		}
	        	}
            	document.add(table);
	        }
	        
	        
	        document.close();
	}
}
